package com.example.animdemo.rigidanim;

public class RigidJointInfo {
	public float time;
	public float t;
	public int   frame;
	public float posX;
	public float posY;
	public float scaleX;
	public float scaleY;
	public float rot;
	public float selfRot;
	public float depth;
	
	public RigidJointInfo()
    {
        time = 0;
        t = 0.5f;
        posX = 0;
        posY = 0;
        scaleX = 0;
        scaleY = 0;
        rot = 0;
        selfRot = 0;
        frame = 0;
        depth = 0f;
    }
	
	public RigidJointInfo(RigidJointInfo orther){
		set(orther);
	}
	
	public final void set(RigidJointInfo f)
    {
        time 	= f.time;
        t 		= f.t;
        frame 	= f.frame;
        posX 	= f.posX;
        posY 	= f.posY;
        scaleX 	= f.scaleX;
        scaleY 	= f.scaleY;
        rot 	= f.rot;
        selfRot = f.selfRot;
        depth 	= f.depth;
    }

    public final void slerp(final RigidJointInfo a,final RigidJointInfo b)
    {
        final float t = (time - a.time) / (b.time - a.time);
        this.slerp(a, b, t);
    }
    
    public final void slerp(final RigidJointInfo a, final RigidJointInfo b, final float t)
    {
        // Check for out-of range parameter and return edge points if so 
        if (t <= 0)
            this.set(a);
        else if (t >= 1)
        	this.set(b);
        else
        {
            if (b.t != 0.5)
            {
				final float omb_t 	= 1 - b.t;
				final float st 		= t * t;
				final float omst 	= (1 - t) * (1 - t);
				final float stt 	= t * (1 - t) * 2;

				if(a.posX != b.posX){
					final float qX = a.posX * (1 - b.t) + b.posX * b.t;
					posX = omst * a.posX + stt * qX + st * b.posX;
				} else 
					posX = a.posX;
				
				if(a.posY != b.posY){
					final float qY = a.posY * (1 - b.t) + b.posY * b.t;
					posY = omst * a.posY + stt * qY + st * b.posY;
				} else 
					posY = a.posY;

				if (a.scaleX != b.scaleX) {
					final float sX = a.scaleX * (1 - b.t) + b.scaleX * b.t;
					scaleX = omst * a.scaleX + stt * sX + st * b.scaleX;
				} else
					scaleX = a.scaleX;
				
				if (a.scaleY != b.scaleY) {
					final float sY = a.scaleY * (1 - b.t) + b.scaleY * b.t;
					scaleY = omst * a.scaleY + stt * sY + st * b.scaleY;
				} else
					scaleY = a.scaleY;

				if (a.rot != b.rot) {

					final float r = a.rot * omb_t + b.rot * b.t;
					rot = omst * a.rot + stt * r + st * b.rot;
				} else
					rot = a.rot;

				if (a.selfRot != b.selfRot) {
					final float sr = a.selfRot * omb_t 	+ b.selfRot * b.t;					
					selfRot = omst * a.selfRot + stt * sr + st * b.selfRot;
				} else
					selfRot = a.selfRot;
				
				if (a.depth != b.depth) {
					final float d  = a.depth * omb_t 	+ b.depth * b.t;
					depth = omst * a.depth + stt * d + st * b.depth;
				} else
					depth = a.depth;
            }
            else
            {
				final float ot = (1 - t);
				
				if(a.posX != b.posX){
					posX = a.posX * ot + t * b.posX;
				} else 
					posX = a.posX;
				
				if(a.posY != b.posY){
					posY = a.posY * ot + t * b.posY;
				} else 
					posY = a.posY;

				if (a.scaleX != b.scaleX) {
					scaleX = a.scaleX * ot + t * b.scaleX;
				} else
					scaleX = a.scaleX;
				
				if (a.scaleY != b.scaleY) {
					scaleY = a.scaleY * ot + t * b.scaleY;
				} else
					scaleY = a.scaleY;

				if(a.rot != b.rot) {
					rot = a.rot * ot + t * b.rot;
				} else
					rot = a.rot;
				
				if (a.selfRot != b.selfRot) {
					selfRot = a.selfRot * ot + t * b.selfRot;
				} else
					selfRot = a.selfRot;
				
				if (a.depth != b.depth) {
					depth = a.depth * ot + t * b.depth;
				} else
					depth = a.depth;
			}
			frame = t < 0.5f ? a.frame : b.frame;
		}
    }

    public static final int compareTime(RigidJointInfo a, RigidJointInfo b)
    {
        if (a.time < b.time) return -1;
        else if (a.time == b.time) return 0;
        return 1;
    }
}
